package com.lzxzjy.serivce.impl;

import com.lzxzjy.mapper.AdminMapper;
import com.lzxzjy.pojo.Admin;
import com.lzxzjy.pojo.PageBean;
import com.lzxzjy.serivce.AdminService;
import com.lzxzjy.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class AdminServiceImpl implements AdminService {
    private final SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public boolean insertAdmin(Admin admin) {
        SqlSession adminSqlSession = factory.openSession();
        AdminMapper adminMapper = adminSqlSession.getMapper(AdminMapper.class);
        Integer integer = adminMapper.insertAdmin(admin);
        adminSqlSession.commit();
        adminSqlSession.close();
        return integer != 0;
    }

    @Override
    public PageBean<Admin> selectAdminByPageAndTerms(Admin admin, int currentPage, int pageSize) {
        SqlSession adminSqlSession = factory.openSession();
        AdminMapper adminMapper = adminSqlSession.getMapper(AdminMapper.class);
        int begin = (currentPage - 1) * pageSize;
        List<Admin> admins = adminMapper.selectAdminByPageAndTerms(admin, begin, pageSize);
        Integer adminTotalCountByTerms = adminMapper.selectAdminTotalCountByTerms(admin);
        PageBean<Admin> adminPageBean = new PageBean<>();
        adminPageBean.setRows(admins);
        adminPageBean.setTotalCount(adminTotalCountByTerms);
        adminSqlSession.close();
        return adminPageBean;
    }

    @Override
    public boolean deleteAdminByIds(int[] adminIds) {
        SqlSession adminSqlSession = factory.openSession();
        AdminMapper adminMapper = adminSqlSession.getMapper(AdminMapper.class);
        Integer integer = adminMapper.deleteByAdminIds(adminIds);
        adminSqlSession.commit();
        adminSqlSession.close();
        return integer != 0;
    }

    @Override
    public boolean deleteAdminById(int adminId) {
        SqlSession adminSqlSession = factory.openSession();
        AdminMapper adminMapper = adminSqlSession.getMapper(AdminMapper.class);
        Integer integer = adminMapper.deleteByAdminId(adminId);
        adminSqlSession.commit();
        adminSqlSession.close();
        return integer != 0;
    }

    @Override
    public boolean updateAdminById(Admin admin) {
        SqlSession adminSqlSession = factory.openSession();
        AdminMapper adminMapper = adminSqlSession.getMapper(AdminMapper.class);
        Integer integer = adminMapper.updateByAdminId(admin);
        adminSqlSession.commit();
        adminSqlSession.close();
        return integer != 0;
    }

    @Override
    public String selectAdminNameByUsernameAndPassWord(String username, String password) {
        SqlSession adminSqlSession = factory.openSession();
        AdminMapper adminMapper = adminSqlSession.getMapper(AdminMapper.class);
        Admin admin = adminMapper.selectAdminNameByUsernameAndPassWord(username, password);
        adminSqlSession.close();
        if(admin != null) {
            return admin.getAdminName();
        }
        return "";
    }
}
